package com.wenan.ui.controls.listBoxes
{
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import mx.collections.ArrayCollection;
	import mx.containers.Canvas;
	import mx.containers.HBox;
	import mx.containers.VBox;
	import mx.controls.Button;
	import mx.controls.Label;
	import mx.controls.dataGridClasses.DataGridColumn;
	import mx.core.ClassFactory;
	
	[Event(name="itemSelected", type="com.wenan.ui.controls.listBoxes.CheckListBoxItemEvent")]
	public class TitleCheckListBox extends VBox
	{
		private var titleLabel:Label;
		private var dataGrid:DataGridSpecial;
		private var dataTemp:ArrayCollection;
		
		private var pageSize:int;
		private var pageno:int = 1;
		private var pageTotle:int;
		private var first:int;
		private var last:int;
		private var dataTotle:int;
		
		public function TitleCheckListBox()
		{
			super();
			
			setStyle("verticalGap", "0");
			setStyle("borderStyle", "solid");
			setStyle("borderColor", "#99a6c8");
			
			initilizeComponents();	
		}
		
		public function get title():String
		{
			return titleLabel.text;
		}
		
		public function set title(value:String):void
		{
			titleLabel.text = value;
		}
		
		[Bindable]
		public function get dataProvider():Object
		{
			return dataGrid.dataProvider;
		}
		
		public function set dataProvider(value:Object):void
		{
			dataGrid.dataProvider = value;
			dataTemp = value as ArrayCollection;
			dataTotle = dataTemp.length;
		}
		
		public function get labelField():String
		{
			return dataGrid.columns[1].dataField;
		}
		
		public function set labelField(value:String):void
		{
			dataGrid.columns[1].dataField = value;
		}
		
		public function get selectedItem():Object
		{
			return dataGrid.selectedItem;
		}
		
		public function set selectedItem(value:Object):void
		{
			dataGrid.selectedItem = value;
		}
		
		public function get selectedItems():Array
		{
			return dataGrid.selectedItems;
		}
		
		public function set selectedItems(value:Array):void
		{
			dataGrid.selectedItems = value;
		}
		
		public function calculatePageSize():void
		{
			pageSize = dataGrid.height / 25;
		}
		
		/**
		 * 初始化控件。
		 */
		private function initilizeComponents():void
		{
			var title:HBox = new HBox();
			title.percentWidth = 100;
			title.height = 24;
			title.setStyle("verticalAlign", "middle");
			title.setStyle("paddingLeft", "8");
			title.styleName = "RepeatBackGround";
			
			titleLabel = new Label();
			title.addChild(titleLabel);
			
			addChild(title);
			
			var client:Canvas = new Canvas();
			client.percentWidth = 100;
			client.percentHeight = 100;
			client.styleName = "GradientColor";
			
			var clientBox:VBox = new VBox();
			clientBox.percentWidth = 100;
			clientBox.percentHeight = 100;
			clientBox.setStyle("verticalGap", "0");
			
			initilizeClientBox(clientBox);
			
			client.addChild(clientBox);
			
			addChild(client);
		}
		
		private function initilizeClientBox(clientBox:VBox):void
		{
			dataGrid = new DataGridSpecial();
			dataGrid.percentWidth = 100;
			dataGrid.percentHeight = 100;
			dataGrid.showHeaders = false;
			dataGrid.setStyle("verticalGridLineColor", "0xD0D4D7");
			dataGrid.verticalScrollPolicy = "off";
			dataGrid.columns = buildDataGridColumns();
			dataGrid.addEventListener(CheckListBoxItemEvent.ITEM_SELECTED, itemSelectedHandler);
			clientBox.addChild(dataGrid);
			
			var scroller:HBox = new HBox();
			scroller.percentWidth = 100;
			scroller.height = 18;
			scroller.setStyle("verticalAlign", "middle");
			scroller.setStyle("horizontalAlign", "center");
			
			var upButton:Button = new Button();
			upButton.styleName = "UpButton";
			upButton.addEventListener(MouseEvent.CLICK, upClickHander);
			scroller.addChild(upButton);
			
			var downButton:Button = new Button();
			downButton.styleName = "DownButton";
			downButton.addEventListener(MouseEvent.CLICK, downClickHander);
			scroller.addChild(downButton);
			
			clientBox.addChild(scroller);
		}
		
		private function buildDataGridColumns():Array
		{
			var columns:Array = [];
			
			var checkColumn:CheckBoxColumn = new CheckBoxColumn();
			checkColumn.width = 25;
			checkColumn.sortable = false;
			checkColumn.headerRenderer = new ClassFactory(CheckBoxHeaderRender);
			checkColumn.itemRenderer = new ClassFactory(CheckBoxItemRenderer);
			columns.push(checkColumn);
			
			var nameColumn:DataGridColumn = new DataGridColumn();
			nameColumn.dataField = "name";
			columns.push(nameColumn);
			
			return columns;
		}
		
		private function itemSelectedHandler(event:Event):void 
		{
			var obj:Object = event;
			dispatchEvent(new CheckListBoxItemEvent(CheckListBoxItemEvent.ITEM_SELECTED, obj.item));
		}
		
		private function upClickHander(event:MouseEvent):void
		{
			pageSize = dataGrid.rowCount;
			if (pageno > 1) {
				pageno--;
				changeDataProvider();
			}
		}
		
		private function downClickHander(event:MouseEvent):void
		{
			pageSize = dataGrid.rowCount;
			pageTotle = (dataTotle + pageSize - 1) / pageSize;
			if (pageno < pageTotle) {
				pageno++;
				changeDataProvider();
			}
		}
		
		private function changeDataProvider():void 
		{
			first = (pageno - 1) * pageSize;
			last = pageno * pageSize -1;
			var temp:ArrayCollection = new ArrayCollection();
			for (var i:int=first; i <dataTotle && i<=last; i++){
				temp.addItem(dataTemp.getItemAt(i));
			}
			dataGrid.dataProvider = temp;
		}
	}
}